package com.koron.css2.newProjectCenter.impl;

import cn.hutool.core.util.ObjectUtil;
import com.koron.css2.baseConfig.bean.BankBean;
import com.koron.css2.baseConfig.mapper.BankMapper;
import com.koron.css2.common.dto.DocDto;
import com.koron.css2.common.service.AbstractExportDoc;
import com.koron.css2.newProjectCenter.bean.PjCollection;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.query.PjCollectionQuery;
import com.koron.css2.newProjectCenter.bean.query.PjMeterQuery;
import com.koron.css2.newProjectCenter.mapper.PjCollectionMapper;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 工程收款-单用户打印水表领用单
 */
@Slf4j
@Service
public class PjChargeDocOneExport extends AbstractExportDoc {

    @Override
    public DocDto getDocText(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjMeterQuery pjMeterQuery = JsonUtils.objectToPojo(req.getData(), PjMeterQuery.class);
            PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
            if (StringUtils.isBlank(pjMeterQuery.getPjCollectionId())){
                throw new RuntimeException("缺少参数");
            }
            PjMeter pjMeter = pjMeterMapper.getSingle(pjMeterQuery);
            if (pjMeter == null) {
                throw new RuntimeException("缺少参数");
            }
            PjMeterList.covertMeterInfo(factory, Arrays.asList(pjMeter));
            PjCollectionMapper pjCollectionMapper = factory.getMapper(PjCollectionMapper.class);
            Map<String, Object> param = new HashMap<>();
            //获取工程信息

            //获取工程信息
            PjCollectionQuery query=new PjCollectionQuery();
            query.setId(pjMeterQuery.getPjCollectionId());
            PjCollection collection = pjCollectionMapper.getSingle(query);
            if (ObjectUtil.isNull(collection)){
                throw new RuntimeException("参数错误:"+pjMeterQuery.getPjCollectionId());
            }

            //设置打印参数
            param.put("projectCode", collection == null ? "" : collection.getProjectCode());
            param.put("ctmName", pjMeter.getCtmInfo() == null ? "" : pjMeter.getCtmInfo().getCtmName());
            param.put("ctmAddr", pjMeter.getCtmInfo() == null ? "" : pjMeter.getCtmInfo().getCtmAddr());
            param.put("setupMeterAddr", pjMeter.getUserInfo() == null ? "" : pjMeter.getUserInfo().getSetupMeterAddr());
            param.put("lastReadingNum", pjMeter.getUserInfo() == null ? "" : pjMeter.getUserInfo().getLastReadingNum());
            param.put("meterBoreName", pjMeter.getMsInfo() == null ? "" : pjMeter.getMsInfo().getMeterBoreName());
            param.put("meterModelName", pjMeter.getMsInfo() == null ? "" : pjMeter.getMsInfo().getMeterModelName());
            param.put("meterNo", pjMeter.getMsInfo() == null ? "" : pjMeter.getMsInfo().getMeterNo());
            param.put("useWaterTypeName", pjMeter.getUserInfo() == null ? "" : pjMeter.getUserInfo().getUseWaterTypeName());
            param.put("openAccountName", pjMeter.getAccountInfo() == null ? "" : pjMeter.getAccountInfo().getOpenAccountName());
            param.put("bankAccount", pjMeter.getAccountInfo() == null ? "" : pjMeter.getAccountInfo().getBankAccount());
            //开户银行
            BankMapper bankMapper = factory.getMapper(BankMapper.class);
            BankBean bank = bankMapper.findBankById(pjMeter.getAccountInfo().getOpenAccountBank());
            if (bank != null) {
                param.put("openAccountBankName", bank.getBankName());
            } else {
                param.put("openAccountBankName", "");
            }

            if (userInfo.getUserInfo() == null) {
                throw new RuntimeException("非法参数");
            }
            param.put("operator", userInfo.getUserInfo().getName());
            param.put("meterBuilderName", pjMeter.getUserInfo() == null ? "" : pjMeter.getUserInfo().getMeterBuilderName());
            param.put("setupMeterDate", pjMeter.getUserInfo() == null ? "" : pjMeter.getUserInfo().getSetupMeterDate());

            //时间处理
            //装表日期
            param.put("year2", "  ");
            param.put("month2", "  ");
            param.put("day2", "  ");
            //开单日期
            LocalDate billingDate = LocalDate.now();
            param.put("year1", billingDate.getYear());
            param.put("month1", billingDate.getMonthValue());
            param.put("day1", billingDate.getDayOfMonth());
            if (StringUtils.isNotBlank(Objects.toString(param.get("setupMeterDate"), ""))) {
                LocalDate setupMeterDate = LocalDate.parse(param.get("setupMeterDate").toString(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                param.put("year2", setupMeterDate.getYear());
                param.put("month2", setupMeterDate.getMonthValue());
                param.put("day2", setupMeterDate.getDayOfMonth());
            }

            DocDto docDto = new DocDto();
            String ctmName = pjMeter.getCtmInfo() == null ? "" : pjMeter.getCtmInfo().getCtmName();
            docDto.setTitle(ctmName + "-水表领用、安装签订单");
            docDto.setTemplate("报装水表单户领用单.docx");
            docDto.setParam(param);
            return docDto;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException("获取水表内容失败。");
        }
    }

}
